package hunt.treasure.scenes;

import hunt.treasure.GameActivity;
import hunt.treasure.config.Define;
import hunt.treasure.config.ImageInfos;
import hunt.treasure.manager.SceneManager;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.ParallelEntityModifier;
import org.andengine.entity.modifier.ScaleModifier;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.util.modifier.IModifier;

public class SplashScene extends AbstractScene {
	private ITextureRegion mSplashTextureRegion;
	private Sprite mSplashSprite;
	private BitmapTextureAtlas mBitmapTextureAtlas;
	private float mScaleSplash = 1.0f;

	@Override
	public void loadResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		mBitmapTextureAtlas = new BitmapTextureAtlas(
				activity.getTextureManager(), ImageInfos.SPLASH_WIDTH,
				ImageInfos.SPLASH_HEIGHT, TextureOptions.BILINEAR);
		mSplashTextureRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(mBitmapTextureAtlas, activity.getAssets(),
						ImageInfos.SPLASH_NAME, 0, 0);
		mBitmapTextureAtlas.load();
		float x = res.camera.getWidth() / 2;
		float y = res.camera.getHeight() / 2;
		// calculate scaled ratio again
		Define.SCALED_RATIO = ((float) GameActivity.CW)
				/ (mSplashTextureRegion.getWidth() * Define.SCALED_ORIGINAL_RESOURCE_RATIO);

		mSplashSprite = new Sprite(x, y, mSplashTextureRegion, res.vbom);
		mScaleSplash = Define.SCALED_RATIO
				* Define.SCALED_ORIGINAL_RESOURCE_RATIO;
		mSplashSprite.setScale(mScaleSplash);
		mSplashSprite.setIgnoreUpdate(true);
	}

	@Override
	public void create() {
		attachChild(mSplashSprite);
	}

	@Override
	public void unloadResources() {
		mSplashSprite.setIgnoreUpdate(false);
		final ParallelEntityModifier modifier = new ParallelEntityModifier(
				new IEntityModifier.IEntityModifierListener() {

					@Override
					public void onModifierStarted(IModifier<IEntity> pModifier,
							IEntity pItem) {
					}

					@Override
					public void onModifierFinished(
							IModifier<IEntity> pModifier, IEntity pItem) {
						mSplashSprite.setVisible(false);
						mSplashSprite.setIgnoreUpdate(true);
						mSplashSprite.detachSelf();
						mSplashTextureRegion.getTexture().unload();
						mBitmapTextureAtlas.unload();
						mBitmapTextureAtlas.clearTextureAtlasSources();
						mSplashSprite = null;
						mSplashTextureRegion = null;
						SceneManager.getInstance().setCurrentScene(null);
						SceneManager.getInstance().showScene(MenuScene.class);
					}
				}, new ScaleModifier(2, 1.5f * mScaleSplash, 0), new MoveModifier(2,
						mSplashSprite.getX(), mSplashSprite.getY(), 0,
						res.camera.getHeight()));

		mSplashSprite.registerEntityModifier(modifier);
	}

	@Override
	public void destroy() {

	}

	@Override
	public void onPause() {

	}

	@Override
	public void onResume() {

	}

}
